﻿#region Copyright © 2013 Paul Miller (Millz)
// All rights are reserved. Reproduction or transmission in whole or in part,
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
// 
// Filename: CachedUnits.cs
// Date:     23/09/2013
// Author:   Paul Miller (Millz)
#endregion

//!CompilerOption:AddRef:System.Runtime.Caching.dll

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Insanity.Core;
using Insanity.Helpers;
using Insanity.Settings;
using JetBrains.Annotations;
using Styx;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace Insanity.Managers
{
    [UsedImplicitly]
    class CachedUnits : CacheManager
    {
        private const int AttackableunitsExpiry = 500;
        private const int PartyMembersExpiry = 10000;

        public static List<WoWUnit> CachedAttackableUnits = new List<WoWUnit>();
        public static List<WoWPlayer> CachedPartyMembers = new List<WoWPlayer>();

        private static List<WoWUnit> UpdateCacheAttackableUnits
        {
            get
            {
                const string cachekey = "AttackableUnits";

                var attackableUnits = Get<List<WoWUnit>>(cachekey);

                if (attackableUnits == null || attackableUnits.Count == 0)
                {
                    attackableUnits = Unit.AttackableUnits.ToList();
                    Add(attackableUnits, cachekey, AttackableunitsExpiry);
                }

                return attackableUnits;
            }
        }

        private static List<WoWPlayer> UpdateCachePartyMembers
        {
            get
            {
                const string cachekey = "PartyMembers";

                var partyMembers = Get<List<WoWPlayer>>(cachekey);

                if (partyMembers == null || partyMembers.Count == 0)
                {
                    partyMembers = StyxWoW.Me.PartyMembers.Union(StyxWoW.Me.RaidMembers).Distinct().ToList();
                    Add(partyMembers, cachekey, PartyMembersExpiry);
                }

                return partyMembers;
            }
        }

        private static void PulseCachedUnits()
        {
            CachedAttackableUnits = UpdateCacheAttackableUnits;
            CachedPartyMembers = UpdateCachePartyMembers;
        }

        public static Composite Pulse
        {
            get
            {
                return new Action(delegate
                {
                    PulseCachedUnits();
                    return RunStatus.Failure;
                });
            }
        }
    }
}
